package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.Enum.ResultEnum;
import com.dongdongshop.core.SpecificPrint;
import com.dongdongshop.pojo.TbSeckillGoods;
import com.dongdongshop.pojo.TbSeckillOrder;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.SeckillOrderServiceI;
import com.dongdongshop.service.SeckillSericeI;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("seckillController")
public class SeckillController {

    @Reference(timeout = 20000000)
    private SeckillSericeI seckillSericeI;

    @Reference(timeout = 20000000)
    private SeckillOrderServiceI seckillOrderServiceI;


    @RequestMapping("toSeckillGoodsList")
    public String toSeckillGoodsList(Model model){
       List<TbSeckillGoods> seckillGoodsList =  seckillSericeI.findSeckillList();
        model.addAttribute("seckillList",seckillGoodsList);
        return "seckill-index";
    }

    //根据id查询秒杀商品
    @RequestMapping("findSeckillGoods/{id}")
    public String findSeckillGoods(@PathVariable("id") long id,Model model){
        TbSeckillGoods seckillGoods = seckillSericeI.findSeckillGoods(id);
        if(null == seckillGoods){
            return "errors";
        }
        model.addAttribute("seckillGoods",seckillGoods);
        return "seckill-item";
    }

    @RequestMapping("submitOrder")
    @ResponseBody
    public SpecificPrint submitOrder(Long seckillId){
        //1.校验用户是否登录
        TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
        if(null == user){
            return SpecificPrint.response(ResultEnum.LOGENINFO);
        }
        try {
            //调用dubbo提交订单，减库存等操作
            seckillOrderServiceI.submitOrder(seckillId,user.getId());
            Map map = new HashMap();
            map.put("seckillId",seckillId);
            map.put("userId",user.getId());
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(map);
        }catch (RuntimeException e){
            //已经卖完
            return SpecificPrint.response(ResultEnum.ERRORS);
        }catch (Exception e){
            return SpecificPrint.response(ResultEnum.ERROR);
        }

    }


    @RequestMapping("toLogins")
    public String toLogins(){
        return "errors";
    }


    @RequestMapping("findOrder")
    @ResponseBody
    public SpecificPrint findOrder(Long seckillId){
        try {
            TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
            TbSeckillOrder seckillOrder = seckillOrderServiceI.findOrder(seckillId,user.getId());
            if(seckillOrder != null){
                return SpecificPrint.response(ResultEnum.SENDASUCCESS).setData("OK");
            }
            return SpecificPrint.response(ResultEnum.FAIL);
        }catch (Exception e){
            return SpecificPrint.response(ResultEnum.fail).setData("buok");
        }
    }
}
